package cn.com.dmg.myspringboot.textAndImg;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.imageio.ImageIO;

import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;


/**
 * @author zhum
 * @ClassName GenerateImgUtil
 * @Description
 * @date 2024/10/12 16:06
 */
public class GenerateImgUtil {

    //public static String baseWorkPath = "C:\\Users\\13117\\Desktop\\1027-多个合集\\";
    public static String baseWorkPath = "E:\\DeskFile\\1028-雌激素\\";

    //生成数量= 边框 * 封面数量

    public static void main(String[] args) throws Exception {

        //batchCreateProductImg();
        //System.out.println(FileUtil.getName(baseWorkPath));

        /*
            需要优化内容
            1. 生成的图片需要进行压缩
            2. 图片去重的时候添加其他装饰
            3.
            @author zhum
            @date 2024/10/22 16:21
         */

        createImgAndContent(1, baseWorkPath);

//        //过渡图片
//        BufferedImage guoDuImage = ImgUtil.read(new File("C:\\Users\\13117\\Desktop\\1728722765516.png"));
//        //边框
//        BufferedImage frameImage = ImgUtil.read(new File("C:\\Users\\13117\\Desktop\\新边框\\新边框1.png"));
//        Img from = Img.from(frameImage);
//
//        from.pressImage(guoDuImage, 0, 0, 1f);
//
//        ImgUtil.write(from.getImg(), new File("C:\\Users\\13117\\Desktop\\新边框\\新边框生成图.png"));

    }

    /**
     * 图文生成
     * @author zhum
     * @date 2024/10/27 14:15
     * @param createCount
     * @param baseWorkPath
     * @return void
     */
    public static void createImgAndContent(int createCount, String baseWorkPath) {
        //背景图片地址
        String srcFilePath = baseWorkPath + "标准背景图片\\";
        //封面生成路径
        String coverImgPath = baseWorkPath + "合成封面图片\\";
        //过渡图片生成路径
        String transitionImgPath = baseWorkPath + "过渡图片\\";
        //一张图片生成地址
        String oneImgPath = baseWorkPath + "一张图片\\";
        //两张图片生成地址
        String twoImgPath = baseWorkPath + "两张图片\\";
        //四张图片生成地址
        String fourImgPath = baseWorkPath + "四张图片\\";


        String imgAndContentPath = baseWorkPath + FileUtil.getName(baseWorkPath) + "-图文内容合集\\";
        String totalImgAndContentPath = baseWorkPath + FileUtil.getName(baseWorkPath) + "-总图文内容合集\\";

        //背景图片（有多少张背景图片就生成多少套图文）
        List<File> srcFileList = FileUtil.loopFiles(new File(srcFilePath));

        int totalCount = 1;
        for (File srcFile : srcFileList) {
            String absolutePath = srcFile.getAbsolutePath();
            batchCreateImg(createCount, absolutePath, coverImgPath, oneImgPath, twoImgPath, fourImgPath, transitionImgPath);

            //产品图片相关
            List<File> productFileList = FileUtil.loopFiles(new File(baseWorkPath + "产品图片合集"));
            //是否生成产品相关图
            boolean isCreateProductImg = false;
            if (productFileList.size() >= 4) {
                isCreateProductImg = true;
            }


            //将图片分组
            List<File> coverImgFileList = FileUtil.loopFiles(coverImgPath);
            List<File> oneImgFileList = FileUtil.loopFiles(oneImgPath);
            List<File> twoImgFileList = FileUtil.loopFiles(twoImgPath);
            List<File> fourImgFileList = FileUtil.loopFiles(fourImgPath);
            for (int i = 1; i <= createCount; i++) {
                int fileNameIndex = 1;
                //路径
                String imgAndContentFilePath = imgAndContentPath + i + "\\";
                //移动封面
                File srcCoverImgFile = coverImgFileList.get(i - 1);
                File destCoverImgFile = new File(imgAndContentFilePath + fileNameIndex++ + "." + FileUtil.getSuffix(srcCoverImgFile));
                FileUtil.copyFile(srcCoverImgFile, destCoverImgFile, StandardCopyOption.REPLACE_EXISTING);
                //移动过渡图片
                Map<String, List<File>> transitionImgFileMap = groupFile(transitionImgPath);
                if (transitionImgFileMap.get(i+"") != null) {
                    List<File> fileList = transitionImgFileMap.get(i+"");
                    for (File srcTransitionImgFile : fileList) {
                        File destTransitionImgFile = new File(imgAndContentFilePath + fileNameIndex++ + "." + FileUtil.getSuffix(srcTransitionImgFile));
                        FileUtil.copyFile(srcTransitionImgFile, destTransitionImgFile, StandardCopyOption.REPLACE_EXISTING);
                    }
                }


                if (isCreateProductImg) {
                    //移动一张的图片
                    File srcOneImgFile = oneImgFileList.get(i - 1);
                    File destOneImgFile = new File(imgAndContentFilePath + fileNameIndex++ + "." + FileUtil.getSuffix(srcOneImgFile));
                    FileUtil.copyFile(srcOneImgFile, destOneImgFile, StandardCopyOption.REPLACE_EXISTING);
                    //移动两张的图片
                    File srcTwoImgFile = twoImgFileList.get(i - 1);
                    File destTwoImgFile = new File(imgAndContentFilePath + fileNameIndex++ + "." + FileUtil.getSuffix(srcTwoImgFile));
                    FileUtil.copyFile(srcTwoImgFile, destTwoImgFile, StandardCopyOption.REPLACE_EXISTING);
                    //移动四张的图片
                    File srcFourImgFile = fourImgFileList.get(i - 1);
                    File destFourImgFile = new File(imgAndContentFilePath + fileNameIndex++ + "." + FileUtil.getSuffix(srcFourImgFile));
                    FileUtil.copyFile(srcFourImgFile, destFourImgFile, StandardCopyOption.REPLACE_EXISTING);
                }

                //移动结尾引导图
                File srcEndImgFile = new File(baseWorkPath + "结尾图.png");
                File destEndImgFile = new File(imgAndContentFilePath + fileNameIndex + "." + FileUtil.getSuffix(srcEndImgFile));
                FileUtil.copyFile(srcEndImgFile, destEndImgFile, StandardCopyOption.REPLACE_EXISTING);

            }
            //生成压缩包
            ZipUtil.zip(imgAndContentPath);

            //删除不需要的文件
            FileUtil.del(coverImgPath);
            FileUtil.del(transitionImgPath);
            FileUtil.del(oneImgPath);
            FileUtil.del(twoImgPath);
            FileUtil.del(fourImgPath);


            //移动到总图文内容合集
            FileUtil.move(new File(imgAndContentPath + "\\1\\"), new File(totalImgAndContentPath + "\\" + totalCount++ +"\\"), true);


        }




        //FileUtil.del(imgAndContentPath);

    }

    /**
     * 批量生成图片
     * @author zhum
     * @date 2024/10/17 17:31
     * @param createCount 生成数量
     * @param srcFilePath 背景图片地址
     * @param coverImgPath 封面目标地址
     * @param oneImgPath 一张图片目标地址
     * @param twoImgPath 两张图片目标地址
     * @param fourImgPath 四张图片目标地址
     * @param transitionImgPath 过渡图片目标地址
     * @return void
     */
    public static void batchCreateImg(int createCount, String srcFilePath, String coverImgPath, String oneImgPath, String twoImgPath, String fourImgPath, String transitionImgPath) {

        //生成的数量主要由过渡图片组成

        //读取所有的边框
        List<File> frameImgList = FileUtil.loopFiles(new File(baseWorkPath + "边框合集"));

        //封面相关
        List<File> coverImgList = FileUtil.loopFiles(new File(baseWorkPath + "封面图片合集"));
        int coverImgCount = 1;


        //最大生成数量为 封面数量 * 边框数量
        if (createCount > frameImgList.size() * coverImgList.size()) {
            createCount = frameImgList.size() * coverImgList.size();
        }

        //过渡图片合集 因为过渡图片可能具有连续性 因此需要进行分组
        String transitionImgSrcPath = baseWorkPath + "过渡图片合集\\";
        Map<String, List<File>> transitionFileMap = groupFile(transitionImgSrcPath);
        int transitionImgGroupCount = 1;

        //产品图片相关
        List<File> productFileList = FileUtil.loopFiles(new File(baseWorkPath + "产品图片合集"));
        //是否生成产品图
        boolean isCreateProductImg = false;
        if (productFileList.size() >= 4) {
            isCreateProductImg = true;
        }
        //图片进行22组合
        List<List<File>> twoImgFileList = null;
        //图片4选4
        List<List<File>> imgFrom4List = null;
        if (isCreateProductImg) {
            //图片进行22组合
            twoImgFileList = get2ImgFrom4(productFileList);
            //图片4选4
            imgFrom4List = get4ImgFrom4(productFileList);
        }
        int oneImgCount = 1;
        int twoImgCount = 1;
        int fourImgCount = 1;

        for (File frameImgFile : frameImgList) {

            //生成封面
            if (coverImgCount <= createCount) {
                for (File coverImgFile : coverImgList) {
                    createImg(srcFilePath, coverImgPath + coverImgCount + ".png", Collections.singletonList(coverImgFile), frameImgFile, 1);
                    coverImgCount ++;
                    if (coverImgCount > createCount) {
                        break;
                    }
                }
            }

            //过渡图片生成
            Set<String> keySet = transitionFileMap.keySet();
            //1组相当于一张图片
            if (transitionImgGroupCount <= createCount) {
                for (String keyName : keySet) {
                    List<File> fileList = transitionFileMap.get(keyName);
                    for (File file : fileList) {
                        createImg(srcFilePath, transitionImgPath + transitionImgGroupCount + "//" + file.getName(), Arrays.asList(file), frameImgFile, ImgLocationModeConstant.TWO_MODE_ONE);
                    }
                    transitionImgGroupCount ++;
                    if (transitionImgGroupCount > createCount) {
                        break;
                    }
                }
            }



            //生成1张图的
            if (oneImgCount <= createCount && isCreateProductImg) {
                for (File productImgFile : productFileList) {
                    createImg(srcFilePath, oneImgPath + oneImgCount + ".png", Collections.singletonList(productImgFile), frameImgFile, 1);
                    oneImgCount ++;
                    if (oneImgCount > createCount) {
                        break;
                    }
                }
            }


            //生成2张图片的
            if (twoImgCount <= createCount && isCreateProductImg) {
                for (List<File> fileList : twoImgFileList) {
                    createImg(srcFilePath, twoImgPath + twoImgCount + ".png", fileList, frameImgFile, ImgLocationModeConstant.TWO_MODE_ONE);
                    twoImgCount ++;
                    if (twoImgCount > createCount) {
                        break;
                    }
                }
            }

            //生成4张图片的
            if (fourImgCount <= createCount && isCreateProductImg) {
                for (List<File> fileList : imgFrom4List) {
                    createImg(srcFilePath, fourImgPath + fourImgCount + ".png", fileList, frameImgFile, 1);
                    fourImgCount ++;
                    if (fourImgCount > createCount) {
                        break;
                    }
                }
            }

        }
    }


    public static Map<String, List<File>> groupFile(String basePath) {
        List<File> fileList = FileUtil.loopFiles(new File(basePath));
        Map<String, List<File>> map = fileList.stream().collect(Collectors.groupingBy(file -> {
            String absolutePath = file.getAbsolutePath();
            String replace = absolutePath.replace(basePath, "");
            return replace.substring(0, replace.lastIndexOf("\\"));
        }));
        return map;
    }
    public static List<List<File>> get4ImgFrom4(List<File> fileList) {
        List<List<File>> list = new ArrayList<>();
        //从四个文件中选出44组合
        list.add(Arrays.asList(fileList.get(0), fileList.get(1), fileList.get(2), fileList.get(3)));
        list.add(Arrays.asList(fileList.get(3), fileList.get(2), fileList.get(1), fileList.get(0)));
        list.add(Arrays.asList(fileList.get(1), fileList.get(0), fileList.get(2), fileList.get(3)));
        list.add(Arrays.asList(fileList.get(0), fileList.get(1), fileList.get(3), fileList.get(2)));
        return list;
    }

    /**
     * 从四个文件中选出22组合
     * @author zhum
     * @date 2024/10/17 14:37
     * @param fileList
     * @return java.util.List<java.util.List<java.io.File>>
     */
    public static List<List<File>> get2ImgFrom4(List<File> fileList) {
        List<List<File>> list = new ArrayList<>();
        //从四个文件中选出22组合
        for (int i = 0; i < fileList.size(); i++) {
            for (int i1 = i+1; i1 < fileList.size(); i1++) {
                List<File> twoFileList = Arrays.asList(fileList.get(i), fileList.get(i1));
                list.add(twoFileList);
            }
        }
        return list;
    }

    /**
     * 添加边框
     * @author zhum
     * @date 2024/10/16 17:20
     * @param from
     * @param frameImgFile
     * @return void
     */

    public static void addFrame(Img from, File frameImgFile) {
        BufferedImage frameImg = ImgUtil.read(frameImgFile);
        from.pressImage(frameImg, 0, 0, 1f);
    }

    public static void createImg(String srcFilePath, String destFilePath, List<File> imgFileList, File frameImgFile, Integer imgLocationMode) {
        AddProductImgBO addProductImg = AddProductImgBO.builder()
                .srcFile(new File(srcFilePath))
                .destFile(new File(destFilePath))
                .firstTile("")
                .firstTileColor(new Color(253, 245, 152))
                .firstTileSize(165)
                .productImgList(imgFileList)
                .text("")
                .textColor(new Color(253, 245, 152))
                .textSize(165)
                .frameImg(frameImgFile)
                .imgLocationMode(imgLocationMode)
                .build();
        handleImg(addProductImg);
    }

    /**
     * 组装图片
     *
     * @param addProductImg
     * @return void
     * @author zhum
     * @date 2024/10/15 16:43
     */
    public static void handleImg(AddProductImgBO addProductImg) {
        //原图片
        BufferedImage srcImg = ImgUtil.read(addProductImg.getSrcFile());
        String srcFileName = addProductImg.getSrcFile().getName();
        String substring = srcFileName.substring(0, srcFileName.indexOf("."));
        String[] split = substring.split("-");

        int oneImgWidth = Integer.parseInt(split[0]);
        int oneImgHeight = Integer.parseInt(split[1]);

        int srcImgWidth = srcImg.getWidth();
        int srcImgHeight = srcImg.getHeight();

        Img from = Img.from(srcImg);

        /**
         * 标准背景图的尺寸：1875 2500
         * 标准产品图的尺寸：685 941
         */

        //商品图片（1张 2张 3张 4张）
        List<File> productImgList = addProductImg.getProductImgList();
        //一张图片的情况
        if (productImgList.size() == 1) {
            File file = productImgList.get(0);
            BufferedImage bufferedImage = ImgUtil.read(file);
            //对图片进行缩放
            Image scaleImg = ImgUtil.scale(bufferedImage, oneImgWidth, oneImgHeight);
            from.pressImage(scaleImg, 0, 0, 1f);
        }

        Integer imgLocationMode = addProductImg.getImgLocationMode();

        //先满足两张的情况
        if (productImgList.size() == 2) {
            if (imgLocationMode == ImgLocationModeConstant.TWO_MODE_ONE) {
                //上下两张
                File file0 = productImgList.get(0);
                BufferedImage bufferedImage0 = ImgUtil.read(file0);
                //对图片进行缩放
                Image scaleImg0 = ImgUtil.scale(bufferedImage0, 1875, 1250);
                from.pressImage(scaleImg0, 0, -625, 1f);

                File file1 = productImgList.get(1);
                BufferedImage bufferedImage1 = ImgUtil.read(file1);
                //对图片进行缩放
                Image scaleImg1 = ImgUtil.scale(bufferedImage1, 1875, 1250);
                from.pressImage(scaleImg1, 0, 625, 1f);
            }

            if (imgLocationMode == ImgLocationModeConstant.TWO_MODE_TWO) {
                File file0 = productImgList.get(0);
                BufferedImage bufferedImage0 = ImgUtil.read(file0);
                //对图片进行缩放
                Image scaleImg0 = ImgUtil.scale(bufferedImage0, 935, 1250);
                from.pressImage(scaleImg0, -468, -625, 1f);

                File file1 = productImgList.get(1);
                BufferedImage bufferedImage1 = ImgUtil.read(file1);
                //对图片进行缩放
                Image scaleImg1 = ImgUtil.scale(bufferedImage1, 935, 1250);
                from.pressImage(scaleImg1, 468, 625, 1f);
            }

            if (imgLocationMode == ImgLocationModeConstant.TWO_MODE_THREE) {
                File file0 = productImgList.get(0);
                BufferedImage bufferedImage0 = ImgUtil.read(file0);
                //对图片进行缩放
                Image scaleImg0 = ImgUtil.scale(bufferedImage0, 935, 1250);
                from.pressImage(scaleImg0, 468, -625, 1f);

                File file1 = productImgList.get(1);
                BufferedImage bufferedImage1 = ImgUtil.read(file1);
                //对图片进行缩放
                Image scaleImg1 = ImgUtil.scale(bufferedImage1, 935, 1250);
                from.pressImage(scaleImg1, -468, 625, 1f);
            }

            //左右各一张
            if (imgLocationMode == ImgLocationModeConstant.TWO_MODE_FOUR) {
                File file0 = productImgList.get(0);
                BufferedImage bufferedImage0 = ImgUtil.read(file0);
                //对图片进行缩放
                Image scaleImg0 = ImgUtil.scale(bufferedImage0, 935, 2500);
                from.pressImage(scaleImg0, -468, -0, 1f);

                File file1 = productImgList.get(1);
                BufferedImage bufferedImage1 = ImgUtil.read(file1);
                //对图片进行缩放
                Image scaleImg1 = ImgUtil.scale(bufferedImage1, 935, 2500);
                from.pressImage(scaleImg1, 468, 0, 1f);
            }

        }


        //三张图片的情况
        if (productImgList.size() == 3) {
            if (imgLocationMode == ImgLocationModeConstant.THREE_MODE_ONE) {
                //上1下2
                File file0 = productImgList.get(0);
                BufferedImage bufferedImage0 = ImgUtil.read(file0);
                //对图片进行缩放
                Image scaleImg0 = ImgUtil.scale(bufferedImage0, 1875, 1250);
                from.pressImage(scaleImg0, 0, -625, 1f);

                File file1 = productImgList.get(1);
                BufferedImage bufferedImage1 = ImgUtil.read(file1);
                //对图片进行缩放
                Image scaleImg1 = ImgUtil.scale(bufferedImage1, 935, 1250);
                from.pressImage(scaleImg1, -468, 625, 1f);

                File file2 = productImgList.get(2);
                BufferedImage bufferedImage2 = ImgUtil.read(file2);
                //对图片进行缩放
                Image scaleImg2 = ImgUtil.scale(bufferedImage2, 935, 1250);
                from.pressImage(scaleImg2, 468, 625, 1f);
            }


            if (imgLocationMode == ImgLocationModeConstant.THREE_MODE_TWO) {
                //上2下1
                File file0 = productImgList.get(0);
                BufferedImage bufferedImage0 = ImgUtil.read(file0);
                //对图片进行缩放
                Image scaleImg0 = ImgUtil.scale(bufferedImage0, 935, 1250);
                from.pressImage(scaleImg0, -468, -625, 1f);

                File file1 = productImgList.get(1);
                BufferedImage bufferedImage1 = ImgUtil.read(file1);
                //对图片进行缩放
                Image scaleImg1 = ImgUtil.scale(bufferedImage1, 935, 1250);
                from.pressImage(scaleImg1, 468, -625, 1f);

                File file2 = productImgList.get(2);
                BufferedImage bufferedImage2 = ImgUtil.read(file2);
                //对图片进行缩放
                Image scaleImg2 = ImgUtil.scale(bufferedImage2, 1875, 1250);
                from.pressImage(scaleImg2, 0, 625, 1f);
            }
        }

        //四张图片的情况
        if (productImgList.size() == 4) {

            File file0 = productImgList.get(0);
            BufferedImage bufferedImage0 = ImgUtil.read(file0);
            //对图片进行缩放
            Image scaleImg0 = ImgUtil.scale(bufferedImage0, 935, 1250);
            from.pressImage(scaleImg0, -468, -625, 1f);

            File file1 = productImgList.get(1);
            BufferedImage bufferedImage1 = ImgUtil.read(file1);
            //对图片进行缩放
            Image scaleImg1 = ImgUtil.scale(bufferedImage1, 935, 1250);
            from.pressImage(scaleImg1, 468, -625, 1f);

            File file2 = productImgList.get(2);
            BufferedImage bufferedImage2 = ImgUtil.read(file2);
            //对图片进行缩放
            Image scaleImg2 = ImgUtil.scale(bufferedImage2, 935, 1250);
            from.pressImage(scaleImg2, -468, 625, 1f);

            File file3 = productImgList.get(3);
            BufferedImage bufferedImage3 = ImgUtil.read(file3);
            //对图片进行缩放
            Image scaleImg3 = ImgUtil.scale(bufferedImage3, 935, 1250);
            from.pressImage(scaleImg3, 468, 625, 1f);
        }


        //添加一级标题
        String firstTile = addProductImg.getFirstTile();

        Font firstTitleFont = new Font("楷体", Font.BOLD, addProductImg.getFirstTileSize());
        List<String> firstTitleList = getRowText(firstTile, srcImgWidth, addProductImg.getFirstTileSize());
        int firstTitleY = -1000;
        for (String firstTitleWord : firstTitleList) {
            from.pressText(firstTitleWord, addProductImg.getFirstTileColor(), firstTitleFont, 0, firstTitleY, 1f);
            firstTitleY += (firstTitleFont.getSize() + 20);
        }

        //添加正文
        String text = addProductImg.getText();
        Font textFont = new Font("黑体", Font.BOLD, addProductImg.getTextSize());
        List<String> textList = getRowText(text, srcImgWidth, addProductImg.getTextSize());
        //正文的y轴偏移量需要加上图片一半的高度
        int textY = 0;
        for (String textWord : textList) {
            from.pressText(textWord, addProductImg.getTextColor(), textFont, 0, textY, 1f);
            textY += (textFont.getSize() + 20);
        }

        //添加边框
//        if (addProductImg.getFrameImg() != null) {
//            addFrame(from, addProductImg.getFrameImg());
//        }
//        File destFile = addProductImg.getDestFile();
//        ImageOutputStream out = ImgUtil.getImageOutputStream(destFile);
//        Image img = from.getImg();
//        Image scale = ImgUtil.scale(img, 200, 400);
//        ImgUtil.write(scale, FileUtil.extName(destFile), out, 0.1f);
        //缩放避免图片过大
        from.scale(0.5f);
        ImgUtil.write(from.getImg(), addProductImg.getDestFile());
    }

    /**
     * 生成硬核科普类文件
     *
     * @param addTextAndImg
     * @return void
     * @author zhum
     * @date 2024/10/13 10:30
     */
    public static void addTextAndImg(AddTextAndImgBO addTextAndImg) {
        //原图片
        BufferedImage srcImg = ImgUtil.read(addTextAndImg.getSrcFile());
        int srcImgWidth = srcImg.getWidth();
        int srcImgHeight = srcImg.getHeight();

        Img from = Img.from(srcImg);

        //添加一级标题
        String firstTile = addTextAndImg.getFirstTile();
//        Font customFont = FontUtil.createFont(new File("C:\\Users\\13117\\Desktop\\阿里巴巴普惠体H.ttf"));
//        // 注册字体
//        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//        ge.registerFont(customFont);

        Font firstTitleFont = new Font("楷体", Font.BOLD, addTextAndImg.getFirstTileSize());
        List<String> firstTitleList = getRowText(firstTile, srcImgWidth, addTextAndImg.getFirstTileSize());
        int firstTitleY = -1000;
        for (String firstTitleWord : firstTitleList) {
            from.pressText(firstTitleWord, addTextAndImg.getFirstTileColor(), firstTitleFont, 0, firstTitleY, 1f);
            firstTitleY += (firstTitleFont.getSize() + 20);
        }

        //添加二级标题
        String secondTile = addTextAndImg.getSecondTile();
        Font secondTitleFont = new Font("黑体", Font.BOLD, addTextAndImg.getSecondTileSize());
        List<String> secondTitleList = getRowText(secondTile, srcImgWidth, addTextAndImg.getSecondTileSize());
        int secondTitleY = firstTitleY + 50;
        for (String secondTitleWord : secondTitleList) {
            from.pressText(secondTitleWord, addTextAndImg.getSecondTileColor(), secondTitleFont, 0, secondTitleY, 1f);
            secondTitleY += (secondTitleFont.getSize() + 20);
        }

        //添加图片
        File addImg = addTextAndImg.getAddImg();
        BufferedImage addImgBuf = ImgUtil.read(addImg);
        //图片缩放比例
        float scale = srcImgWidth / 2f / addImgBuf.getWidth();
        //对图片进行缩放（放大）
        Image scaleImg = ImgUtil.scale(addImgBuf, scale);
        //缩放后图片的高度的一半
        int halfAddImgHeight = (addImgBuf.getHeight() / 2 * (int) scale);
        //添加图片的y轴偏移量需要加上 缩放后图片的一半高度
        int addImgY = secondTitleY + 50 + halfAddImgHeight;
        from.pressImage(scaleImg, 0, addImgY, 1f);


        //添加正文
        String text = addTextAndImg.getText();
        Font textFont = new Font("黑体", Font.BOLD, addTextAndImg.getTextSize());
        List<String> textList = getRowText(text, srcImgWidth, addTextAndImg.getTextSize());
        //正文的y轴偏移量需要加上图片一半的高度
        int textY = addImgY + 200 + halfAddImgHeight;
        for (String textWord : textList) {
            from.pressText(textWord, addTextAndImg.getTextColor(), textFont, 0, textY, 1f);
            textY += (textFont.getSize() + 20);
        }

        //添加边框
        File frameImgFile = addTextAndImg.getFrameImg();
        BufferedImage frameImg = ImgUtil.read(frameImgFile);
        from.pressImage(frameImg, 0, 0, 1f);

        ImgUtil.write(from.getImg(), addTextAndImg.getDestFile());
    }

    private static List<String> getRowText(String text, int imgWidth, int fontSize) {
        fontSize += 10;
        //每行文字的数量
        int rowWordNum = imgWidth / fontSize;
        //一共多少行
        int rowNum = text.length() / rowWordNum;
        if (text.length() % rowWordNum != 0) {
            rowNum++;
        }
        //截取文字
        List<String> list = new ArrayList<>();
        for (int i = 0; i < rowNum; i++) {
            if ((i + 1) * rowWordNum > text.length()) {
                list.add(text.substring(i * rowWordNum));
            } else {
                list.add(text.substring(i * rowWordNum, (i + 1) * rowWordNum));
            }
        }
        return list;
    }


    public static void createImg() throws Exception {
        String firstTitle = "肝肾虚的表现";
        String secondTitle = "表现一";
        String content = "肝肾虚的表现通常有:腰膝酸软、四肢疲乏、口千舌燥、眼干、黑眼圈重、面色发黄、失眠多梦、情绪不佳等。";


        String templateImgPath = "C:\\Users\\13117\\Desktop\\test1012-2.png";
        String addImgPath = "C:\\Users\\13117\\Desktop\\1728722765516.png";

        String outPath = "C:\\Users\\13117\\Desktop\\test1012-3.png";
        String word = "关于眼睛，你不知道的8个问题！";
        // 读取原始图片
        BufferedImage originalImage = ImageIO.read(new File(templateImgPath));

        //计算图片的中心横坐标
        int centerX = originalImage.getWidth() / 2;

        int initX = 642;//centerX - (7*224);
        int initY = 500;
        // 创建Graphics2D对象来绘制文字
        Graphics2D g2d = originalImage.createGraphics();

        // 设置字体、颜色和大小
        Font font = new Font("阿里巴巴普惠体-粗体", Font.ITALIC, 146);
        g2d.setFont(font);
        g2d.setColor(Color.orange); // 设置颜色为白色


        //g2d.drawString(secondTitle, initX, initY);

        BufferedImage addImage = ImageIO.read(new File(addImgPath));
        g2d.drawImage(addImage, 700, 700, null);

        // 释放图形上下文使用的系统资源
        g2d.dispose();

        // 将结果写入新的文件
        ImageIO.write(originalImage, "png", new File(outPath));
    }

}
